package com.ossjk.api.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.attendance.entity.Atdreport;
import com.ossjk.oa.attendance.entity.Atdtable;
import com.ossjk.oa.attendance.entity.Attendance;
import com.ossjk.oa.attendance.entity.Vemp2Dept;
import com.ossjk.oa.attendance.service.IAtdreportService;
import com.ossjk.oa.attendance.service.IAtdtableService;
import com.ossjk.oa.attendance.service.IAttendanceService;
import com.ossjk.oa.attendance.vo.AtdtableVo;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IPostService;
import com.ossjk.oa.v_emp2dept.entity.V_emp2dept;
import com.ossjk.oa.v_emp2dept.service.V_emp2deptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Api(description = "考勤报表接口")
@RestController
@RequestMapping("/api/attendance")
public class AttendanceApiController extends BaseController {
    @Autowired
    private IAtdreportService iAtdreportService;
    @Autowired
    private IAtdtableService iAtdtableService;
    @Autowired
    private IAttendanceService iAttendanceService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private V_emp2deptService v_emp2deptService;

    /**
     * 6.  考勤报表
     * 6.1 根据pc端逻辑 由[考勤] + [请假] + [外出]   生成考勤报表，维度是个人/月      根据pc端逻辑,统计好考勤信息
     * <p>
     * <p>
     * 6.4 个人可以在此提交月度考勤结果，提交后不能再次修改，作为算考勤工资的依据
     *
     * @author 林镇彬
     */
//6.1生成考勤报表
    @GetMapping("getSelfkq")
    @ApiOperation(value = "查询个人月度考勤记录", notes = "两个参数必填，年和月")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "month", value = "月", paramType = "query", dataType = "String", required = true)
    })
    public ResponseBean<List<Atdtable>> listSelf(String year, String month) {
        Wrapper wrapper = Condition.create();
//        获取员工id
        String eid = getCurrnetUserId();
        wrapper.eq("eid", eid);
        if (!CommonUtil.isBlank(year)) {
            wrapper.eq("year", year);
        }
        if (!CommonUtil.isBlank(month)) {
            wrapper.eq("month", month);

        }
        return (ResponseBean<List<Atdtable>>) responseSuccess(iAtdtableService.selectList(wrapper));
    }

    /**
     * @param year
     * @param month 6.4个人可以在此提交月度考勤结果，提交后不能再次修改，作为算考勤工资的依据
     * @return
     * @author:林镇彬
     */


    @ApiOperation(value = "修改确认请求状态为1", notes = "IsConfirmed状态：1，已确认，2未确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "month", value = "月", paramType = "query", dataType = "String", required = true),
    })
    @PutMapping("confirmAtdTable")
    public ResponseBean confirmAtdTable(String year, String month) {
//        查询出状态为2未确认的数据
        Wrapper wrapper = Condition.create().isWhere(true).eq("a.isdelete", "1").eq("a.IsConfirmed", 2);
//        获取当前员工的eid
        String eid = getCurrnetUserId();
//        eid加入查询条件
        wrapper.eq("a.eid", eid);
        if (!CommonUtil.isBlank(year)) {
            wrapper.eq("a.year", year);
        }
        if (!CommonUtil.isBlank(month)) {
            wrapper.eq("a.month", month);
        }

        AtdtableVo atdtableVo = iAtdtableService.selectVo(wrapper);
//        修改IsConfirmed状态为1 就是确认了
        atdtableVo.setIsConfirmed(new BigDecimal(1));
        if (iAtdtableService.updateById(atdtableVo)) {
            return (ResponseBean) responseSuccess();
        } else {
            return (ResponseBean) responseFail();
        }

    }

    /**
     * 考勤报表
     * 6.2 月度导航 ([上一月][本月][下一月])
     *
     * @author 林国标
     */
    //
    @GetMapping("/AttendanceNavigation")
    @ApiImplicitParam(name = "mth", value = "年月", paramType = "query", dataType = "String")
    @ApiOperation(value = "考勤月度导航", notes = "考勤月度导航")
    public Object AttendanceNavigation(String mth) {


        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
        return  responseSuccess(iAtdtableService.selectPreNext(mth));
    }
    /**
     * 考勤报表
     *二级部门领导
     *
     * @author 林国标
     */
    //

    @ApiOperation(value = "考勤列表", notes = "考勤列表")

    @GetMapping("getKqApplication")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "eid", value = "用户", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "did", value = "部门id", paramType = "query", dataType = "String")
    })

    public ResponseBean<List<Atdtable>> getLeaveApplication(String year, String month, String eid, String did) {
//        1、判断前端时候有传递，部门id，员工id，月份,如果都没有可以通过token获取用户id
        if (CommonUtil.isBlank(month)) {
            Calendar now = Calendar.getInstance();
            month = CommonMethod.calMth(0, now.getTime()); // 默认本月
        }
        if (CommonUtil.isBlank(eid)) {
            //默认自己的
            eid = getCurrnetUserId();
        }

//        2、通过视图v_emp2dept获取用户的部门和level
        Wrapper wrapper = Condition.create().isWhere(true).eq("id", eid);

        Vemp2Dept vemp2Dept = iAtdreportService.selectDaoHao(wrapper);

        Wrapper wrapper2 = Condition.create().like("year", year);

//        3、判断level：=2:显示部门所有信息，>2: 显示个人，=1：总经理：功能中要有一个部门id的参数查询
        if (vemp2Dept.getLevel() > 2) {
            //>2: 显示个人
            wrapper2.eq("eid", eid).like("month", month);
        } else if (vemp2Dept.getLevel() == 2) {
            //=2:显示部门所有信息
            wrapper2.like("month", month).eq("did", vemp2Dept.getDid());
        } else {
            wrapper2.like("month", month).eq("did", did).eq("eid", eid);
        }

//        4、	就可以获取到	部门id，员工id   了
//        5、查询数据，并且返回
        return (ResponseBean<List<Atdtable>>) responseSuccess(iAtdtableService.selectList(wrapper2));
    }




    @ApiOperation(value = "查询所有部门信息", notes = "查询所有部门信息")
    @GetMapping("AttendanceSelectAllDept")
    public ResponseBean<List<V_emp2dept>> AttendanceSelectAllDept(){
        Wrapper wrapper1=Condition.create();

        return (ResponseBean<List<V_emp2dept>>) responseSuccess(iDepartmentService.selectAllDept(wrapper1)) ;
    }

    @ApiOperation(value = "按照传入的部门id查询其部门下的所有人信息", notes = "按照传入的部门id查询其部门下的所有人信息")
    @ApiImplicitParam(name = "did", value = "部门id", paramType = "query", dataType = "String", required = true)
//    查询每个部门人数接口
    @GetMapping("AttendanceSelectDeptPer")
    public ResponseBean<List<V_emp2dept>> AttendanceSelectDeptPer(String did){
        Wrapper wrapper1=Condition.create().isWhere(true).eq("did",did);
        return (ResponseBean<List<V_emp2dept>>) responseSuccess(v_emp2deptService.selectByPer(wrapper1)) ;
    }

}
